-- micro codigo

library IEEE;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
USE ieee.numeric_std.all;

ENTITY micro_codigo IS
	PORT
	(
		ir					: IN	STD_LOGIC_VECTOR(7 downto 0);
		tc					: IN	STD_LOGIC_VECTOR(7 downto 0);
		microbus			: OUT	STD_LOGIC_VECTOR(15 downto 0)
	);
END micro_codigo;

ARCHITECTURE arch_mc OF micro_codigo IS
	type arreglo is array(0 to 31) of STD_LOGIC_VECTOR(0 to 15);
	signal microcode : arreglo;
BEGIN
	
	microcode(0)	<= "0000000000000001";
	microcode(1)	<= "0000000000000000";
	microcode(2)	<= "0000000000000000";
	microcode(3)	<= "0000000000000000";
	
	microcode(4)	<= "0000100010000001";
	microcode(5)	<= "0000000000000000";
	microcode(6)	<= "0000000000000000";
	microcode(7)	<= "0000000000000000";
	
	microcode(8)	<= "0000110000000001";
	microcode(9)	<= "0000000000000000";
	microcode(10)	<= "0000000000000000";
	microcode(11)	<= "0000000000000000";
	
	microcode(12)	<= "0110000000000010";
	microcode(13)	<= "0000100110100001";
	microcode(14)	<= "0000000000000000";
	microcode(15)	<= "0000000000000000";
	
	microcode(16)	<= "0110000000000010";
	microcode(17)	<= "0000101110100001";
	microcode(18)	<= "0000000000000000";
	microcode(19)	<= "0000000000000000";
	
	microcode(20)	<= "0110000000000010";
	microcode(21)	<= "0000000000101010";
	microcode(22)	<= "0110000001000010";
	microcode(23)	<= "0000100110000001";
	
	microcode(24)	<= "0110000000000010";
	microcode(25)	<= "0000000000101010";
	microcode(26)	<= "0011000000000010";
	microcode(27)	<= "1000000001000001";
	
	microcode(28)	<= "0110000000000010";
	microcode(29)	<= "0000000000010001";
	microcode(30)	<= "0000000000000000";
	microcode(31)	<= "0000000000000000";
	
	
	micro_codigo_process:
	PROCESS(tc, ir, microcode)
	BEGIN
		if (tc = "00000000") then
			-- DR <- M[PC]
			-- TC <- TC+1
			microbus <= "0110000000000010";
		elsif (tc = "00000001") then
			-- IR <- DR
			-- PC <- PC+1
			-- TC <- TC+1
			microbus <= "0000000000100110";
		elsif ( (to_integer(unsigned(ir)) < 8) AND (to_integer(unsigned(ir)) >= 0) AND (to_integer(unsigned(tc)) >= 0) AND (to_integer(unsigned(tc)) <= 5)) then
			microbus <= microcode(to_integer(unsigned(ir))*4 + to_integer(unsigned(tc))-2);
		else
			microbus <= "0000000000000000";
		end if;

	END PROCESS micro_codigo_process;

END arch_mc;
